/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Servlet;

import clases.Conexion;
import clases.Error_Data;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Alfredo
 */
public class PagoCheque extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println(\"<html>\");
            out.println(\"<head>\");
            out.println(\"<title>Servlet PagoCheque</title>\");
            out.println(\"</head>\");
            out.println(\"<body>\");
            out.println(\"<h1>Servlet PagoCheque at \" + request.getContextPath () + \"</h1>\");
            out.println(\"</body>\");
            out.println(\"</html>\");
            */
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate=\"collapsed\" desc=\"HttpServlet methods. Click on the + sign on the left to edit the code.\">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        //String operacion=request.getParameter(\"pageOperation\");
        String verfima=request.getParameter("verfirma");
        String cuentacheque = request.getParameter("cuentacheque");
        String chequera=request.getParameter("chequera");
        String cheque=request.getParameter("cheque");
        String monto = request.getParameter("monto");
        Double mont = Double.parseDouble( monto );
        String pagar=request.getParameter("pagar");

        HttpSession sesionOk = request.getSession();
        String empleado = (String)sesionOk.getAttribute("Usuario");


        if(verfima!=null){
            pintarfirma(request,response,cuentacheque,chequera,cheque,monto);
        }
         else if(pagar!=null){
            Conexion nueva=new Conexion();

            Error_Data nuevo =new Error_Data();




            if(nuevo.Solo_Numeros3(cuentacheque) && nuevo.Solo_Numeros3(cheque) && nuevo.Solo_Numeros3(chequera) && nuevo.Solo_Numeros3(monto) && mont > 0){

            boolean realizado = nueva.PagarCheque(cuentacheque,chequera,cheque,mont,empleado);

            if(realizado)
            {
                //fue eliminado con exito
                nuevo.EscribirMensajes("Cheque Depositado", response,"Continuar","DepositosMonetarios.jsp");

            }else{

                nuevo.EscribirMensajes("Error al tratar de Depositar Cuenta", response,"Regresar","Monetarios.jsp");

            }
            }
             else{
                nuevo.EscribirMensajes("Error al tratar de Depositar Cuenta", response,"Regresar","Monetarios.jsp");

             }

         }

        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void pintarfirma(HttpServletRequest request, HttpServletResponse response,String cuentacheque, String chequera, String cheque, String monto) throws IOException {

                PrintWriter out = response.getWriter();
                response.setHeader("Pragma","no-cache");
                response.setHeader("Cache-Control","no-store");
                response.setHeader("Cache", "no-cache");
                response.setDateHeader("Expires",0);





                String usuario = "";
                HttpSession sesionOk = request.getSession();
                if (sesionOk.getAttribute("Usuario") == null) {

                out.println("<jsp:forward page=\"index.jsp\">"+
                "<jsp:param name=\"error\" value=\"Identifiquese primero\"/>"+
                "</jsp:forward>");

                } else {
                usuario = (String)sesionOk.getAttribute("Usuario");
                }



                out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\""+
                   "\"http://www.w3.org/TR/html4/loose.dtd\">"+

                "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"+
                "<html xmlns=\"http://www.w3.org/1999/xhtml\">"+
                "<head>"+
                "<title>Pago Cheques</title>"+
                "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />"+
                "<link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\" />"+
                "<script type=\"text/javascript\" src=\"js/jquery-1.3.2.min.js\"></script>"+
                "<script type=\"text/javascript\" src=\"js/script.js\"></script>"+
                "<script type=\"text/javascript\" src=\"js/cufon-yui.js\"></script>"+
                "<script type=\"text/javascript\" src=\"js/arial.js\"></script>"+
                "<script type=\"text/javascript\" src=\"js/cuf_run.js\"></script>"+
                "<meta http-equiv==\"Pragma\" content=\"no-cache\"/>"+
                "<meta http-equiv=\"Expires\" content=\"-1\"/>"+
                "<meta http-equiv==\"Cache-Control\" content=\"no-store\"/>"+
                "<meta http-equiv=\"Cache\" content=\"no-cache\"/>"+
                "<script lenguaje=\"JavaScript\">"+
                "if(window.history.forward(1) != null)"+
                    "window.history.forward(1);");
                out.println("</script>"+
                "</head>"+
                "<body>"+
                "<div class=\"main\">"+
                "  <div class=\"header\">"+
                "    <div class=\"header_resize\">"+
                 "     <div class=\"logo\">"+
                  "      <h1><a href=\"#\"><span>Siempre</span> a su lado</a></h1>"+
                   "   </div>"+
                    "  <div class=\"menu_nav\">"+
                     "   <ul>"+

                          "<li> <a href=\"gerente.jsp\">Principal</a></li>"+
                          "<li> <a href=\"cerrarsesion.jsp\">Cerrar sesión</a></li>"+
                         "</ul>"+
                      "</div>"+
                      "<div class=\"clr\"></div>"+
                      "<div class=\"hbg\"><img src=\"images/header_images.jpg\" width=\"400\" height=\"275\" alt=\"\" class=\"fl\" />"+
                       " <div class=\"info fl\"> "+
                        "  <h3>Banco Central <br /> "+
                         "   Uniendo al mundo </h3> "+
                        "</div>" +
                        "<div class=\"clr\"></div>"+
                      "</div>"+
                      "<div class=\"clr\"></div>"+
                    "</div>"+
                  "</div>"+
                  "<div class=\"content\">"+
                    "<div class=\"content_resize\">"+
                      "<div class=\"mainbar\">"+
                       " <div class=\"article\">"+
                          "<h2><span>Ingreso Datos</span></h2>");

                            if(request.getParameter("error")!=null)
                            {
                            out.println(request.getParameter("error"));
                            }

                          out.println("<div class=\"clr\"></div>"+
                         "</div>"+
                        "<div class=\"article\">"+
                         "<div class=\"clr\"></div>"+
                          "<form action="+Conexion.localhost+"PagoCheque method=\"post\">"+
                            "<ol>"+
                             "<li> Ingrese No. Cuenta del Cheque"+
                              "   <input name=\"cuentacheque\" type=\"text\" value=\""+cuentacheque+"\" align=\"center\"><br>"+
                              "   <input type=\"submit\" name=\"verfirma\" id=\"verfirma\" value=\"Ver Firma\" align=\"center\" ><br>"+
                              " <img src=\"C:/apache-tomcat-6.0.32/bin/imagenes/"+imagen(cuentacheque)+"\" width=\"400\" height=\"275\" alt=\"\"/>"+
                             "</li>"+
                             "<li> Ingrese No. Chequera"+
                                 "<input type=\"text\" id=\"chequera\" name=\"chequera\" value=\""+chequera+"\"align=\"center\"><br>"+
                             "</li>"+
                             "<li> Ingrese No. Cheque"+
                                 "<input type=\"text\" name=\"cheque\" value=\""+cheque+"\"align=\"center\"><br>"+
                             "</li>"+
                             "<li> Ingrese Monto"+
                                 "<input type=\"text\" name=\"monto\" value=\""+monto+"\"align=\"center\"><br>"+
                             "</li>"+
                             "<li>"+
                               "<input type=\"submit\" id=\"pagar\" name=\"pagar\" value=\"Pagar\" align=\"center\" ><br>"+
                               "<div class=\"clr\"></div>"+
                              "</li>"+
                            "</ol>"+
                          "</form>"+
                        "</div>"+
                      "</div>"+
                      "<div class=\"sidebar\">"+
                        "<div class=\"gadget\">"+
                          "<h2 class=\"star\"><span>Nuestro</span> Menu</h2>"+
                          "<div class=\"clr\"></div>"+
                          "<ul class=\"sb_menu\">"+
                            "<li><a href=\"cerrarsesion.jsp\">Cerrar Sesion</a></li>"+
                            "<li><a href=\"Monetarios.jsp\">Monetarios</a></li>"+
                            "<li><a href=\"ChequesP.jsp\">Cheques Propios</a></li>"+
                            "<li><a href=\"ChequesO.jsp\">Cheques Otros</a></li>"+
                          "</ul>"+
                        "</div>"+
                      "</div>"+
                      "<div class=\"clr\"></div>"+
                    "</div>"+
                  "</div>"+

                  "<div class=\"footer\">"+
                    "<div class=\"footer_resize\">"+
                     " <p class=\"lf\">&copy; Copyright <a href=\"#\">MyWebSite</a>.</p>"+
                      "<p class=\"rf\">Layout by I <a href=\"http://www.iwebsitetemplate.com/\">Website Templates</a></p>"+
                      "<div class=\"clr\"></div>"+
                    "</div>"+
                  "</div>"+
                "</div>"+
                "</body>"+
                "</html>");

    }

    private String imagen(String cuentacheque) {
        Conexion nueva = new Conexion();
        return nueva.trimagen(cuentacheque);
    }

}
